import init_ai from './ai';
import { IActions, IEvents } from './interfaces';

interface IOptions {
	query: any;		// 当前小程序的 query
	path: string;	// 当前小程序的页面地址
}

export default function init_app(events: IEvents, actions: IActions) {
	const data = {} as any;
	const ai = init_ai(data, actions, events);
	return {
		fd: data,
		async onLaunch(options: IOptions) {
			data.app = this;
			data.params = options.query || {};
			// Object.freeze(data);
			await ai.emit('fd-events-init');
			// Do something initial when launch.
			ai.emit('fd-events-ali-app-launch', options);
		},
		onShow(options: IOptions) {
			// Do something when show.
			// 页面加载完成 一个页面只会调用一次，代表页面已经准备妥当，可以和视图层进行交互。对界面的设置，如 my.setNavigationBar 请在 onReady 之后设置。
			data.params = options.query;
			ai.emit('fd-events-ali-app-show', options);
		},
		onHide() {
			// Do something when hide.
			ai.emit('fd-events-ali-app-hide');
		},
		onError(msg: any) {
			console.error(msg);
			ai.emit('fd-events-ali-app-error');
		}
	};
}
